package com.lge.pitchingcoins.ui.clients;

import org.cocos2d.layers.CCMultiplexLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;

import com.lge.pitchingcoins.ui.clients.layer.ClientsMainLayer;
import com.lge.pitchingcoins.ui.clients.layer.GamePlayForClientsLayer;
import com.lge.pitchingcoins.util.Util;

public class ClientsMainActivityWithCocos extends Activity {
    private CCGLSurfaceView mGLSurfaceView = null;
    private CCMultiplexLayer layer = null;

    private String nickname = null;


    // Getters and Setters
    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }


    // Callback Methods
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mGLSurfaceView = new CCGLSurfaceView(this);
        setContentView(mGLSurfaceView);

        mGLSurfaceView.setKeepScreenOn(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

        CCDirector.sharedDirector().onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();

        CCDirector.sharedDirector().attachInView(mGLSurfaceView);
        CCDirector.sharedDirector().setLandscape(false);
        CCDirector.sharedDirector().setDisplayFPS(Util.showFPS);
        CCDirector.sharedDirector().setAnimationInterval(1.0f / 60);

        CCScene scene = CCScene.node();

        layer = CCMultiplexLayer.node(new ClientsMainLayer(this), new GamePlayForClientsLayer(this));
        layer.setTag(0);
        scene.addChild(layer, 0);

        CCDirector.sharedDirector().runWithScene(scene);
    }

    @Override
    protected void onPause() {
        super.onPause();

        CCDirector.sharedDirector().onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        CCDirector.sharedDirector().end();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            if (layer != null) {
                if (layer.getTag() == 0) {
                    return super.onKeyDown(keyCode, event);
                }
            }
        }

        return CCDirector.sharedDirector().onKeyDown(event);
    }

    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        return super.dispatchKeyEvent(event);
    }
}